package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.dao.UserRoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.pojo.UserRole;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public User findByName(String username) {
        User user = new User();
        user.setUsername(username);
        log.info("进入了后端");
        User user1 = userDao.selectOne(user);
        if (user1==null){
            return null;
        }
        Integer id = user1.getId();
        List<Role> roles = roleDao.findById(id);
        user1.getRoles().addAll(roles);
        for (Role role : roles) {
            List<Permission> permissions = permissionDao.findById(role.getId());
            role.getPermissions().addAll(permissions);
        }
        log.info("[UserService]user1:{}",user1);
        return user1;
    }

    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> page = userDao.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult();
        pageResult.setRows(page.getResult());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    public int add(User user, Integer[] roles) {
        int i = userDao.insertSelective(user);
        log.info("[UserService] user1:{},roles;{}",user,roles);
        User byName = userDao.findByName(user.getUsername());
        for (Integer role : roles) {
            UserRole userRole = new UserRole();
            userRole.setRoleId(role);
            userRole.setUserId(byName.getId());
            userRoleDao.insertSelective(userRole);
        }
        return i;
    }

    @Override
    public User findById(Integer id) {
        User byId = userDao.findById(id);
        return byId;
    }

    @Override
    public int edit(User user,Integer[] roles) {
        int i = userDao.updateByPrimaryKey(user);
        userRoleDao.deleteByUserId(user.getId());
        for (Integer role : roles) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(role);
            userRoleDao.insertSelective(userRole);
        }
        return i;
    }

    @Override
    public int delete(Integer id) {
        List byUserRoleId = userRoleDao.findByUserRoleId(id);
        int deleteUserRole = userRoleDao.deleteByUserId(id);
        int deleteUser = userDao.deleteByUserId(id);
        return deleteUser;
    }
}
